﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsEntityPropertyCreateDataCommand : CmsEntityPropertyCreateCommand
    {
        public override CmsEntityMetaField ExecuteOne()
        {
            CmsEntityMetaField result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsEntityMetaField>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsEntityMetaField>;

            if (currentUnitOfWork != null)
            {
                var data = SqlUtility<CmsEntityMetaField, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_CreateEntityProperty",
                        CommandParameter,
                        DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsEntityMetaField, int>>());

                if (data != null)
                {
                    result = data.FirstOrDefault();
                }
            }

            return result;
        }
    }
}
